package com.solo.common.core.utils.collection;

import cn.hutool.core.collection.CollUtil;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 集合工具类，扩展hutool
 * @author 十一
 * @since 2024/12/12
 * 人生若只如初见，何事秋风悲画扇
 **/
public class CollUtils extends CollUtil {

    public static <T> Set<T> toSet(Collection<T> from) {
        return toSet(from, v -> v);
    }

    /**
     * 将集合转换为 HashSet
     * @param from 被转换的值
     * @param func 被转换的字段
     * @return {@link Set }<{@link U }>
     */
    public static <T, U> Set<U> toSet(Collection<T> from, Function<T, U> func) {
        if (CollUtil.isEmpty(from)) {
            return new HashSet<>();
        }
        return from.stream().map(func).filter(Objects::nonNull).collect(Collectors.toSet());
    }

    public static <T, U> Set<U> toSet(Collection<T> from, Function<T, U> func, Predicate<T> filter) {
        if (CollUtil.isEmpty(from)) {
            return new HashSet<>();
        }
        return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toSet());
    }

}
